function add_new_cat()
{
	var name = document.getElementById('cat-name').value;
	var description = document.getElementById('cat-description').value;
	var state = document.getElementById('new_state').value;
	var parentid = document.getElementById('parentid').value;

	if (window.XMLHttpRequest) {
		xmlhttp = new window.XMLHttpRequest();
	}
	else {
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == '4' && xmlhttp.status == '200') {
			response = xmlhttp.responseText;
			var error = document.getElementById('error-message');
			if (response == 1) {
				error.innerHTML = 'Category name is required!';
				error.setAttribute('class','warning');
			} 
			else if (response == 2) {
				error.innerHTML = 'Category\'s state must lower than parent\'s state!';
				error.setAttribute('class','error');
			}
			else {
				error.innerHTML = '1 category created successful';
				error.setAttribute('class','success');
				//Reload the categories dropdown list
				reload_cats_list();
				//Clear the value
				document.getElementById('cat-name').value = '';
				document.getElementById('cat-description').value = '';
				document.getElementById('new_state').value = 1;
				document.getElementById('parentid').value = 0;
				var conditions = get_conditions();
				//Reload the categories table
				var viewcats = document.getElementById('viewcategories');
				if (viewcats) {
					reload_cats_table(conditions,1,5);
				}
			}
		}
	}
	//Create paramaters
	parameters = 'name=' + name + '&parentid=' + parentid + '&description=' + description + '&state=' + state + '&action=create';

	xmlhttp.open('POST','scripts/get-categories.php',true);
	xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	xmlhttp.send(parameters);
	return false; //Don't submit form
}
//Reload categories list
function reload_cats_list()
{
	if (window.XMLHttpRequest) {
			xmlhttp = new window.XMLHttpRequest();
		}
		else {
			xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
		}
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == '4' && xmlhttp.status == '200') {
				var response = xmlhttp.responseText;
				var parentid = document.getElementById('parentid');
				var categoryname = document.getElementById('categoryname');
				if (categoryname) {
					categoryname.innerHTML = response;
				}
				response = '<option value="0">No parent</option>' + response;
				parentid.innerHTML = response;
			}
		}
		//Create paramaters
		parameters = '&action=reloadlist';

		xmlhttp.open('POST','scripts/get-categories.php',true);
		xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		xmlhttp.send(parameters);
		return false; //Don't submit form
}
//Get the conditions
function get_conditions()
{
	var cat_level = document.getElementById('cat_level').value;
	var cat_state = document.getElementById('cat_state').value;
	var cat_author = document.getElementById('cat_author').value;
	var conditions = '';
	if (cat_level !== '0') {
		conditions = conditions + 'c.cat_level<=' + cat_level + ' AND ';
	}
	if (cat_state == '0') {
		conditions = conditions + 'c.state<>3 AND ';
	}
	else {
		conditions = conditions + 'c.state=' + cat_state + ' AND ';
	}
	if (cat_author !== 'all') {
		conditions = conditions + 'c.created_by=\'' + cat_author + '\' AND ';
	}
	if (conditions !== '') {
		var len = conditions.length;
		conditions = conditions.substring(0,len - 5);
		conditions = 'WHERE ' + conditions;
	}
	//If choose trash then change trash button to delete button
	if (cat_state == '3') {
		var trash = document.getElementById('trash');
		if (trash)
			trash.parentNode.innerHTML = '<a onclick="delete_cats();" id="delete" class="toolbar" href="#"><span class="delete"></span>Delete</a>';
	}
	else {
		var del = document.getElementById('delete');
		if (del)
			del.parentNode.innerHTML = '<a onclick="update_cats_state(this);" id="trash" class="toolbar" href="#"><span class="trash"></span>Trash</a>';
	}
	return conditions;
}
//Change the conditions
function change_conditions()
{
	var conditions = get_conditions();
	reload_cats_table(conditions,1,5);
	var error = document.getElementById('error-message');
	error.innerHTML = '';
	error.setAttribute('class','');
}
//Reload categories table
function reload_cats_table(conditions,page,cats_per_page)
{
	if (window.XMLHttpRequest) {
		xmlhttp = new window.XMLHttpRequest();
	}
	else {
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == '4' && xmlhttp.status == '200') {
			var response = xmlhttp.responseText;
			var viewcats = document.getElementById('viewcategories');
			viewcats.innerHTML = response;
		}
	}
	//Create paramaters
	parameters = 'conditions= ' + conditions + '&page=' + page + '&cats_per_page=' + cats_per_page + '&action=reload';

	xmlhttp.open('POST','scripts/get-categories.php',true);
	xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	xmlhttp.send(parameters);
	return false; //Don't submit form
}
//Reload the page
function changePage(obj)
{
	var conditions = get_conditions();
	var page = obj.text;
	reload_cats_table(conditions,page,5);
}
//Click new button
function new_category()
{
	document.getElementById('cat-header').innerHTML = 'Create new category';
	document.getElementById('cat-name').value = '';
	document.getElementById('cat-description').value = '';
	document.getElementById('new_state').value = 1;
	var submit = document.getElementById('submit');
	submit.parentNode.innerHTML = '<input type="button" onclick="add_new_cat(\'2\');" value="Add new" class="myButton" id="submit">';
	reload_cats_list();
}
//Update category state
function update_cat_state(obj)
{
	var id = obj.parentNode.parentNode.firstChild.firstChild.value;
	var cur_state = obj.firstChild.innerHTML;
	var state = 1;
	if (cur_state == 'publish') {
		state = 2;
	}

	if (window.XMLHttpRequest) {
		xmlhttp = new window.XMLHttpRequest();
	}
	else {
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == '4' && xmlhttp.status == '200') {
			response = xmlhttp.responseText;
			if (response == 0) {
				var error = document.getElementById('error-message');
				error.innerHTML = 'Category\' state must be lower than parent\'s state';
				error.setAttribute('class','error');
			}
			else {
				var error = document.getElementById('error-message');
				if (response == 1)
					error.innerHTML = '1 category updated successful';
				else
					error.innerHTML = response + ' categories updated successful';
				error.setAttribute('class','success');
				//Reload the categories dropdown list
				reload_cats_list();
				//Clear the value
				document.getElementById('cat-name').value = '';
				document.getElementById('cat-description').value = '';
				document.getElementById('new_state').value = 1;
				document.getElementById('parentid').value = 0;
				var conditions = get_conditions();
				//Reload the categories table
				var viewcats = document.getElementById('viewcategories');
				if (viewcats) {
					reload_cats_table(conditions,1,5);
				}
			}
		}
	}
	//Create paramaters
	parameters = 'id=' + id + '&state=' + state + '&action=updatestate';

	xmlhttp.open('POST','scripts/get-categories.php',true);
	xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	xmlhttp.send(parameters);
	return false; //Don't submit form
}
//Click check
function isChecked(value)
{
	if (value == false){
		document.getElementById('checkall').checked = false;
	}
	else {
		var list = document.getElementsByName('cb[]');
		var len = list.length;
		for (var i = 0; i < len; i++)
			if (list[i].checked == false)
				break;
		if (i == len)
			document.getElementById('checkall').checked = true;
	}
}
//Click check all
function checkAll(obj)
{
	var list = document.getElementsByName('cb[]');
	var len = list.length;
	for (var i = 0; i < len; i++)
		list[i].checked = obj.checked;
}
//Update categories state
function update_cats_state(obj)
{
	var btn = obj.id;
	var state = 1;
	if (btn == 'unpublish')
		state = 2;
	else if (btn == 'trash') 
		state = 3;
	var ids = '';
	var list = document.getElementsByName('cb[]');
	var len = list.length;
	for (var i = 0; i < len; i++) {
		if (list[i].checked == true)
			ids = ids +list[i].value + ',';
	}
	if (ids == '') {
		var error = document.getElementById('error-message');
		error.innerHTML = 'Please choose the categories!';
		error.setAttribute('class','warning');
		return;
	}
	len = ids.length;
	ids = ids.substring(0, len - 1);

	if (window.XMLHttpRequest) {
		xmlhttp = new window.XMLHttpRequest();
	}
	else {
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == '4' && xmlhttp.status == '200') {
			response = xmlhttp.responseText;
			if (response == 0) {
				var error = document.getElementById('error-message');
				error.innerHTML = 'Category\' state must be lower than parent\'s state';
				error.setAttribute('class','error');
			}
			else {
				var error = document.getElementById('error-message');
				if (response == 1)
					error.innerHTML = '1 category updated successful';
				else
					error.innerHTML = response + ' categories updated successful';
				error.setAttribute('class','success');
				//Reload the categories dropdown list
				reload_cats_list();
				var conditions = get_conditions();
				//Reload the categories table
				var viewcats = document.getElementById('viewcategories');
				if (viewcats) {
					reload_cats_table(conditions,1,5);
				}
			}
		}
	}
	//Create paramaters
	parameters = 'ids=' + ids + '&state=' + state + '&action=updatecatsstate';

	xmlhttp.open('POST','scripts/get-categories.php',true);
	xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	xmlhttp.send(parameters);
	return false; //Don't submit form
}
//Click categery title link
function editCategory(obj)
{
	var id = obj.parentNode.parentNode.firstChild.firstChild.value;
	send_cat_request(id);
}
//Click Edit button -> Edit the first choose
function edit_category()
{
	var list = document.getElementsByName('cb[]');
	var len = list.length;
	for (var i = 0; i < len; i++) {
		if (list[i].checked == true)
			break;
	}
	if (i == len) {
		var error = document.getElementById('error-message');
		error.innerHTML = 'Please choose the categories';
		error.setAttribute('class','warning');
		return;
	}
	send_cat_request(list[i].value);
}
//Send get category request
function send_cat_request(id)
{
	if (window.XMLHttpRequest) {
		xmlhttp = new window.XMLHttpRequest();
	}
	else {
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == '4' && xmlhttp.status == '200') {
			response = xmlhttp.responseText;
			if (response == 0) {
				var error = document.getElementById('error-message');
				error.innerHTML = 'Can not get the category\'s informations. Please try again';
				error.setAttribute('class','error');
			}
			else {
				var edit_panel = document.getElementById('rightsection');
				edit_panel.innerHTML = response;
			}
		}
	}
	//Create paramaters
	parameters = 'id=' + id + '&action=edit';

	xmlhttp.open('POST','scripts/get-categories.php',true);
	xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	xmlhttp.send(parameters);
	return false; //Don't submit form
}
function update_category_info()
{
	var id = document.getElementById('cat_id').value;
	var name = document.getElementById('cat-name').value;
	var description = document.getElementById('cat-description').value;
	var state = document.getElementById('new_state').value;
	var parent_id = document.getElementById('parentid').value;
	//Sent the request
	if (window.XMLHttpRequest) {
		xmlhttp = new window.XMLHttpRequest();
	}
	else {
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == '4' && xmlhttp.status == '200') {
			response = xmlhttp.responseText;
			var error = document.getElementById('error-message');
			if (response == 0) {
				error.innerHTML = 'There\'s nothing to be updated!';
				error.setAttribute('class','warning');
			}
			else if (response == -1) {
				error.innerHTML = 'Category\'s state must be lower than parent\'s state!';
				error.setAttribute('class','error');
			}
			else {
				error.innerHTML = 'The category updated successful!';
				error.setAttribute('class','success');
				//Get conditions
				var conditions = get_conditions();
				reload_cats_table(conditions,1,5);
				new_category();
			}
		}
	}
	//Create paramaters
	parameters = 'id=' + id + '&name=' + name + '&state=' + state + '&description=' + description + '&parent_id=' + parent_id + '&action=update';

	xmlhttp.open('POST','scripts/get-categories.php',true);
	xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	xmlhttp.send(parameters);
	return false; //Don't submit form
}
//Change display order 
function display_order(dir,obj)
{
	var id = obj.parentNode.parentNode.firstChild.firstChild.value;
	//Sent the request
	if (window.XMLHttpRequest) {
		xmlhttp = new window.XMLHttpRequest();
	}
	else {
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == '4' && xmlhttp.status == '200') {
			response = xmlhttp.responseText;
			var error = document.getElementById('error-message');
			if (response == 0) {
				error.innerHTML = 'Can\' not change the order. Please try again!';
				error.setAttribute('class','error');
			}
			else {
				if (response == 1)
					error.innerHTML = '1 category updated successful!';
				else
					error.innerHTML = response + ' categories updated successful!';
				error.setAttribute('class','success');
				//Get conditions
				var conditions = get_conditions();
				reload_cats_table(conditions,1,5);
			}
		}
	}
	//Create paramaters
	parameters = 'id=' + id + '&dir=' + dir + '&action=changeorder';

	xmlhttp.open('POST','scripts/get-categories.php',true);
	xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	xmlhttp.send(parameters);
	return false; //Don't submit form
}
//Delete the categories
function delete_cats()
{
	var list = document.getElementsByName('cb[]');
	var ids = '';
	var len = list.length;
	for (var i = 0; i < len; i++) {
		if (list[i].checked == true)
			ids = ids + list[i].value + ','
	}
	if (ids == '') {
		error.innerHTML = 'Please choose the category!';
		error.setAttribute('class','warning');
		return;
	}
	len = ids.length;
	ids = ids.substring(0,len - 1);
	//Sent the request
	if (window.XMLHttpRequest) {
		xmlhttp = new window.XMLHttpRequest();
	}
	else {
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == '4' && xmlhttp.status == '200') {
			response = xmlhttp.responseText;
			var error = document.getElementById('error-message');
			if (response == 0) {
				error.innerHTML = 'Can\' not delete the categories. Please try again!';
				error.setAttribute('class','error');
			}
			else {
				if (response == 1)
					error.innerHTML = '1 category deleted successful!';
				else
					error.innerHTML = response + ' categories deleted successful!';
				error.setAttribute('class','success');
				//Get conditions
				var conditions = get_conditions();
				reload_cats_table(conditions,1,5);
			}
		}
	}
	//Create paramaters
	parameters = 'ids=' + ids + '&action=delete';

	xmlhttp.open('POST','scripts/get-categories.php',true);
	xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	xmlhttp.send(parameters);
	return false; //Don't submit form
}